<template>
  <el-dialog
    v-model="visible"
    title="订单详情"
    width="1000px"
    :before-close="handleClose"
    center
    :body-style="{ overflow: 'auto', padding: '20px', overflowY: 'auto' }"
  >
    <div v-if="order" class="order-detail">
      <!-- 基本信息 -->
      <el-card class="detail-section">
        <template #header>
          <span class="section-title">基本信息</span>
        </template>
        <el-descriptions :column="3" border>
          <el-descriptions-item label="订单号">
            {{ order.orderNo }}
          </el-descriptions-item>
          <el-descriptions-item label="客户名称">
            {{ order.customerName }}
          </el-descriptions-item>
          <el-descriptions-item label="订单状态">
            <el-tag :type="getOrderStatusType(order.status)">
              {{ getOrderStatusLabel(order.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="支付状态">
            <el-tag :type="getPaymentStatusType(order.paymentStatus)">
              {{ getPaymentStatusTag(order.paymentStatus) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="交货日期">
            {{ formatDate(order.deliveryDate) }}
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">
            {{ formatDateTime(order.createTime) }}
          </el-descriptions-item>
          <el-descriptions-item label="备注" :span="3">
            {{ order.remark || '无' }}
          </el-descriptions-item>
        </el-descriptions>
      </el-card>

      <!-- 产品信息 -->
      <el-card class="detail-section">
        <template #header>
          <span class="section-title">产品信息</span>
        </template>
        <el-table :data="order.products" border>
          <el-table-column prop="productName" label="产品名称" />
          <el-table-column prop="specification" label="规格" />
          <el-table-column prop="quantity" label="数量" width="80" />
          <el-table-column prop="unit" label="单位" width="60" />
          <el-table-column prop="unitPrice" label="单价" width="100">
            <template #default="{ row }"> {{ formatAmount(row.unitPrice) }} </template>
          </el-table-column>
          <el-table-column prop="totalPrice" label="小计" width="120">
            <template #default="{ row }"> {{ formatAmount(row.totalPrice) }} </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 金额信息 -->
      <el-card class="detail-section">
        <template #header>
          <span class="section-title">金额信息</span>
        </template>
        <div class="amount-info">
          <div class="amount-row">
            <span class="label">订单总额：</span>
            <span class="value">{{ formatAmount(order.totalAmount) }}</span>
          </div>
          <div class="amount-row">
            <span class="label">优惠金额：</span>
            <span class="value discount">-{{ formatAmount(order.discountAmount) }}</span>
          </div>
          <div class="amount-row final">
            <span class="label">实付金额：</span>
            <span class="value">{{ formatAmount(order.finalAmount) }}</span>
          </div>
        </div>
      </el-card>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import {
  formatAmount,
  formatDate,
  formatDateTime,
  getOrderStatusLabel,
  getPaymentStatusTag,
} from '@/utils'
import type { Order } from '@/types'

interface Props {
  modelValue: boolean
  order: Order | null
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value),
})

// 获取订单状态类型
const getOrderStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    pending: 'warning',
    confirmed: 'primary',
    production: 'info',
    delivered: 'success',
    cancelled: 'danger',
  }
  return statusMap[status] || 'info'
}

// 获取支付状态类型
const getPaymentStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    unpaid: 'danger',
    partial: 'warning',
    paid: 'success',
  }
  return statusMap[status] || 'info'
}

const handleClose = () => {
  visible.value = false
}
</script>

<style scoped>
.order-detail {
  height: 100%;
  overflow-y: auto;

  .detail-section {
    margin-bottom: 20px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .section-title {
    font-weight: 600;
    color: #303133;
  }

  .amount-info {
    .amount-row {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 8px 0;
      border-bottom: 1px solid #f0f0f0;

      &:last-child {
        border-bottom: none;
      }

      &.final {
        font-size: 16px;
        font-weight: 600;
        color: #e6a23c;
      }

      .label {
        color: #606266;
      }

      .value {
        font-weight: 500;

        &.discount {
          color: #f56c6c;
        }
      }
    }
  }
}

/* 自定义滚动条样式 */
:deep(.el-dialog__body) {
  scrollbar-width: thin;
  scrollbar-color: #c1c1c1 #f1f1f1;
}

:deep(.el-dialog__body::-webkit-scrollbar) {
  width: 8px;
}

:deep(.el-dialog__body::-webkit-scrollbar-track) {
  background: #f1f1f1;
  border-radius: 4px;
}

:deep(.el-dialog__body::-webkit-scrollbar-thumb) {
  background: #c1c1c1;
  border-radius: 4px;
}

:deep(.el-dialog__body::-webkit-scrollbar-thumb:hover) {
  background: #a8a8a8;
}
</style>
